<?php

namespace app\admin\service\recharge;

use app\admin\model\recharge\BoxRechargeConfig;
use app\admin\service\BaseService;

class BoxRechargeConfigService extends BaseService
{

    public function __construct()
    {
        $this->model = new BoxRechargeConfig();
    }

    /**
     * Desc : getList
     * User : Steven
     * Date : 2024-05-28 14:00
     * @return array
     */
    public function getList(): array
    {
        $params = request()->param();
        $limit = $params['limit'] ?? 10;
        $where  = [];
        $time = $params['time_range'] ?? [];
        $start = $time[0] ?? '';
        $end = $time[1] ?? '';
        if (!empty($start) && !empty($end)) {
            $where[] = ['g.create_time', 'between', [$start, $end]];
        }

        $status = $params['status'] ?? "";
        if (isset($status) && !empty($status)) {
            $where[] = ['g.status', '=', $status];
        }
        if (!empty($params['game_id'])) {
            $where[] = ['g.game_id', '=', $params['game_id']];
        }
        $where[]  = ['g.mark','=',1];
        $arr   = $this->model->alias('g')
            ->leftJoin('cq_box_game b', 'b.id = g.game_id')
            ->field('g.*,b.game_name')
            ->where($where)
            ->paginate($limit)->toArray();
        $arr['data'] = array_map(function ($item) {
            if(empty($item['extends'])){
                unset($item['extends']);
            }else{
                $item['extends'] = json_decode($item['extends'], true) ?? '';
            }
            return $item;
        }, $arr['data']);
        return [
            'code'  => 0,
            'msg'   => 'success',
            'count' => $arr['total'] ?? 0,
            'data'  => $arr['data'] ?? [],
        ];
    }

    public function edit()
    {
        $params = request()->param();
        if (isset($params['extends'])) {
            if(empty($params['extends'])){
                $params['extends'] = '';
            }else{
                $params['extends'] = json_encode($params['extends']) ?: '';
            }
        }
        $params['ip_whitelist'] = $params['ip_whitelist'] ?? '';
        return parent::newEdit($params,false,true); // TODO: Change the autogenerated stub
    }

}